﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

using KamLib.Web.MVC;


namespace KamLib.Web.Bootstrap
{


    public class NavBar : MvcElement
    {

        public List<DropDown> Items;
        public bool TopFixed;


        public delegate NavBar Setter(bool topFixed = false, params Action<DropDown.Setter>[] items);

        public NavBar Set(bool topFixed = false, params Action<DropDown.Setter>[] items)
        {
            Items = items.ToElements(Items);
            TopFixed = topFixed;
            return this;
        }

        public static implicit operator NavBar(Action<Setter> setterAction)
        {
            var mdl = new NavBar();
            if (setterAction == null) return mdl;
            Setter setter = mdl.Set;
            setterAction(setter);
            return mdl;
        }

    }


    public static class NavBarHelper
    {

        public static NavBar Items(this NavBar me, params Action<DropDown.Setter>[] items)
        {
            me.Items = items.ToElements(me.Items);
            return me;
        }

        public static NavBar Items(this NavBar me, params DropDown[] items)
        {
            if (items.no()) return me;
            if (me.Items == null)
                me.Items = new List<DropDown>();
            me.Items.AddRange(items);
            return me;
        }


        public static NavBar Item(this NavBar me, Action<Link.Setter> title, Action<DropDownMenu.Setter> menu)
        {
            me.Items(new DropDown { Title = title, Menu = menu, });
            return me;
        }

        public static NavBar Item(this NavBar me, Action<Link.Setter> title, params Action<DropDownItem.Setter>[] items)
        {
            me.Items(new DropDown
            {
                Title = title,
                Menu = new DropDownMenu { Items = items.ToElements() },
            });
            return me;
        }

        public static NavBar Item(this NavBar me, MvcText title, params Action<DropDownItem.Setter>[] items)
        {
            var el = new DropDown
            {
                Title = new Link { Text = title, },
                Menu = new DropDownMenu { Items = items.ToElements() },
            };
            me.Items(el);
            return me;
        }

    }


    partial class BootstrapHelper
    {

        #region For MVC

        public MvcElement NavBar(Action<NavBar.Setter> setter)
        {
            return NavBar((NavBar)setter);
        }

        #endregion


        #region Renders

        [Browsable(false)]
        public virtual NavBar NavBar(NavBar el)
        {
            if (el == null) return null;

            el.Tag("div").Class("navbar");
            if (el.TopFixed)
                el.Class("navbar-fixed-top");

            foreach (var item in el.Items)
            {
                item.Tag = "li";
                if (item.Title.no())
                    item.Class("divider-vertical");
                else
                    DropDown(item);
            }

            el.Renderer = sb =>
            {
                sb.Append(@"<div class=""navbar-inner""><div class=""container""><ul class=""nav"">");
                foreach (var item in el.Items)
                {
                    item.Render(sb);
                }
                sb.Append(@"</ul></div></div>");
            };

            return el;
        }

        #endregion

    }


}
